<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input clearable class="filter-item" style="width: 200px;" v-model="keyword" placeholder="请输入菜单名称"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="handleFind">查找</el-button>
      <el-button type="primary" @click="handleAdd" icon="el-icon-edit">添加</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%" stripe v-loading="Loading">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-table border :data="scope.row.childCategoryMenu" :show-header="showHeader" style="width: 100%">
            <el-table-column label="序号" width="60" align="center">
              <template slot-scope="scope">
                <span>({{scope.$index + 1}})</span>
              </template>
            </el-table-column>

            <el-table-column label="菜单名称" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.name }}</span>
              </template>
            </el-table-column>

            <el-table-column label="菜单级别" align="center">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.menuLevel=='1'" type="success" size="small" effect="plain">一级菜单</el-tag>
                <el-tag v-if="scope.row.menuLevel=='2'" type="primary" size="small" effect="plain">二级菜单</el-tag>
              </template>
            </el-table-column>

            <el-table-column label="菜单简介" min-width="200" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.summary }}</span>
              </template>
            </el-table-column>

            <el-table-column label="图标" align="center">
              <template slot-scope="scope">
                <i :class="scope.row.icon" />
              </template>
            </el-table-column>

            <el-table-column label="路由" width="200" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.path }}</span>
              </template>
            </el-table-column>

            <el-table-column label="是否显示" width="100" align="center">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.hidden==1" type="success" size="small" effect="plain">是</el-tag>
                <el-tag v-if="scope.row.hidden==0" type="info" size="small" effect="plain">否</el-tag>
              </template>
            </el-table-column>

            <el-table-column label="创建时间" min-width="160" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.createTime }}</span>
              </template>
            </el-table-column>

            <el-table-column label="状态" align="center">
              <template slot-scope="scope">
                <template v-if="scope.row.status == 1">
                  <span>正常</span>
                </template>
                <template v-if="scope.row.status == 2">
                  <span>推荐</span>
                </template>
                <template v-if="scope.row.status == 0">
                  <span>已删除</span>
                </template>
              </template>
            </el-table-column>

            <el-table-column label="操作" min-width="230" align="center">
              <template slot-scope="scope">
                <el-button @click="handleStick(scope.row)" type="warning" size="small" plain>置顶</el-button>
                <el-button @click="handleEdit(scope.row)" type="primary" size="small" plain>编辑</el-button>
                <el-button @click="handleDelete(scope.row)" type="danger" size="small" plain>删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>

      <el-table-column label="序号" width="60" align="center">
        <template slot-scope="scope">
          <span>{{scope.$index + 1}}</span>
        </template>
      </el-table-column>

      <el-table-column label="菜单名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="菜单级别" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.menuLevel=='1'" type="success" size="small" effect="plain">一级菜单</el-tag>
          <el-tag v-if="scope.row.menuLevel=='2'" type="primary" size="small" effect="plain">二级菜单</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="菜单简介" min-width="200" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.summary }}</span>
        </template>
      </el-table-column>

      <el-table-column label="图标" align="center">
        <template slot-scope="scope">
          <i :class="scope.row.icon" />
        </template>
      </el-table-column>

      <el-table-column label="路由" width="200" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.path }}</span>
        </template>
      </el-table-column>

      <el-table-column label="是否显示" width="100" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.hidden==1" type="success" size="small" effect="plain">是</el-tag>
          <el-tag v-if="scope.row.hidden==0" type="info" size="small" effect="plain">否</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" min-width="160" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <template v-if="scope.row.status == 1">
            <span>正常</span>
          </template>
          <template v-if="scope.row.status == 2">
            <span>推荐</span>
          </template>
          <template v-if="scope.row.status == 0">
            <span>已删除</span>
          </template>
        </template>
      </el-table-column>

      <el-table-column label="操作" min-width="230" align="center">
        <template slot-scope="scope">
          <el-button @click="handleStick(scope.row)" type="warning" size="small">置顶</el-button>
          <el-button @click="handleEdit(scope.row)" type="primary" size="small">编辑</el-button>
          <el-button @click="handleDelete(scope.row)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-if="dialogFormVisible" :visible.sync="dialogFormVisible" :before-close="handleDialogClose" width="600px" :close-on-click-modal="false">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="菜单名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="菜单等级" :label-width="formLabelWidth" prop="menuLevel">
          <el-select v-model="form.menuLevel" placeholder="请选择" v-if="isEditForm" disabled>
            <el-option label="一级菜单" value="1"></el-option>
            <el-option label="二级菜单" value="2"></el-option>
          </el-select>
          <el-select v-model="form.menuLevel" placeholder="请选择" v-else>
            <el-option label="一级菜单" value="1"></el-option>
            <el-option label="二级菜单" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item v-if="form.menuLevel == '2'" label="父菜单名" :label-width="formLabelWidth" prop="parentUid">
          <el-select v-model="form.parentUid" filterable clearable placeholder="请输入父菜单名">
            <el-option v-for="item in menuOptions" :key="item.uid" :label="item.name" :value="item.uid"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item v-if="form.menuLevel == '1'" label="定向地址" :label-width="formLabelWidth" prop="redirect">
          <el-input v-model="form.redirect" auto-complete="off" placeholder="地址前面需要'/'">
          </el-input>
        </el-form-item>

        <el-form-item label="路由地址" :label-width="formLabelWidth" prop="path">
          <el-input v-model="form.path" auto-complete="off" placeholder="一级菜单前面需要'/'，二级不需要">
          </el-input>
        </el-form-item>

        <el-form-item label="组件路径" :label-width="formLabelWidth" prop="component">
          <el-input v-model="form.component" auto-complete="off" placeholder="一级统一为'Layout'，二级菜单前面需要'/'">
          </el-input>
        </el-form-item>

        <el-form-item label="菜单图标" :label-width="formLabelWidth" prop="icon">
          <el-button :icon="form.icon" @click="openIconsDialog('prefix-icon')">点击选择</el-button>
        </el-form-item>

        <el-form-item label="菜单介绍" :label-width="formLabelWidth" prop="summary">
          <el-input v-model="form.summary" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="是否隐藏" :label-width="formLabelWidth" prop="hidden">
          <el-radio-group v-model="form.hidden" size="small">
            <el-radio-button label="1" border>是</el-radio-button>
            <el-radio-button label="0" border>否</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleDialogClose()">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <icons-dialog :visible.sync="iconsVisible" :current="form.icon" @select="setIcon" />
  </div>
</template>
<script>
import {
  getAllMenu,
  addMenu,
  editMenu,
  deleteMenu,
  stickMenu,
} from "@/api/categoryMenu";
import IconsDialog from "../../components/IconsDialog";
export default {
  components: {
    IconsDialog,
  },
  data() {
    return {
      Loading: true,
      tableData: [],
      keyword: "",
      currentPage: 1,
      pageSize: 10,
      total: 0, //总数量
      title: "增加菜单",
      dialogFormVisible: false, //控制弹出框
      formLabelWidth: "80px",
      isEditForm: false,
      showHeader: false, //是否显示表头
      iconsVisible: false, // 是否显示icon选择器
      menuOptions: [], //一级菜单候选项
      form: {
        uid: "",
        parentUid: "",
        name: "",
        summary: "",
        redirect: "",
        component: "",
        icon: "el-icon-star-off",
        menuLevel: "1",
        path: "",
        sort: 0,
        menuType: '0', //菜单类型  菜单
        status: 1,
        hidden: 0
      },
      rules: {
        name: [
          { required: true, message: "菜单名称不能为空", trigger: "blur" },
          { min: 1, max: 20, message: "长度在1到20个字符" },
        ],
        menuLevel: [
          { required: true, message: "菜单等级不能为空", trigger: "blur" },
        ],
        parentUid: [
          { required: true, message: "父级菜单不能为空", trigger: "blur" },
        ],
        summary: [
          { required: true, message: "菜单简介不能为空", trigger: "blur" },
        ],
        icon: [{ required: true, message: "菜单图标不能为空", trigger: "blur" }],
        path: [{ required: true, message: "路由地址不能为空", trigger: "blur" }],
        hidden: [
          { required: true, message: "是否隐藏不能为空", trigger: "blur" },
        ],
        menuType: [
          { required: true, message: "菜单类型不能为空", trigger: "blur" },
        ],
        component: [
          { required: true, message: "组件路径不能为空", trigger: "blur" },
        ],
        redirect: [
          { required: true, message: "定向地址不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    // this.getDictList();
    this.menuList();
  },
  methods: {
    menuList() {
      this.Loading = true;
      var params = new URLSearchParams();
      params.append("keyword", this.keyword);
      getAllMenu(params).then((response) => {
        this.tableData = response.data;
        this.menuOptions = response.data;
        this.Loading = false;
      });
    },
    handleFind() {
      this.menuList();
    },
    handleAdd() {
      this.title = "增加菜单";
      this.isEditForm = false;
      this.dialogFormVisible = true;
    },
    handleStick(row) {
      this.$confirm("此操作将会把该标签放到首位, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {};
          params.uid = row.uid;
          stickMenu(params).then((response) => {
            this.menuList();
            this.$notify({
              title: '成功',
              message: response.data,
              type: 'success'
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消置顶",
          });
        });
    },
    handleEdit(row) {
      this.dialogFormVisible = true;
      this.isEditForm = true;
      // var parentUid = row.parentUid;
      this.title = "编辑菜单";
      Object.assign(this.form, row);
    },
    handleDelete(row) {
      this.$confirm("此操作将把菜单删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {};
          params.uid = row.uid;
          deleteMenu(params).then((response) => {
            this.$message({
              type: "success",
              message: response.data,
            });
            this.menuList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.isEditForm) {
            editMenu(this.form).then((response) => {
              this.$notify({
                title: '成功',
                message: response.data,
                type: 'success'
              });
              this.handleDialogClose();
              this.menuList();
            });
          } else {
            addMenu(this.form).then((response) => {
              this.$notify({
                title: '成功',
                message: response.data,
                type: 'success'
              });
              this.handleDialogClose();
              this.menuList();
            });
          }
        }
      });
    },
    handleDialogClose() {
      this.form = {
        uid: "",
        name: "",
        summary: "",
        icon: "el-icon-star-off",
        redirect: "",
        component: "",
        menuLevel: "1",
        path: "",
        sort: 0,
        menuType: '0', //菜单类型  菜单
        status: 1,
        hidden: 0
      };
      this.dialogFormVisible = false;
    },
    openIconsDialog(model) {
      this.iconsVisible = true;
      this.currentIconModel = model;
    },
    // 选择图标
    setIcon(val) {
      this.form.icon = val;
    },
  },
};
</script>

<style>
/* 二级表格对称 */
.el-table__expanded-cell[class*="cell"] {
  padding: 10px 0 10px 47.4px;
}
</style>